//
//  STSetSloganController.swift
//  SlowTrajectory
//
//  Created by iOS-Czz on 2023/10/23.
//

import UIKit
import HXPhotoPicker_Lite
import WMZDialog
class STSetSloganController: STProjectBaseController {
    @IBOutlet var sloganView: UIView!
    @IBOutlet var dateView: UIView!
    @IBOutlet var photoView: UIView!
    var selectImage:UIImage? = nil
    @IBOutlet var sloganTF: UITextField!
    @IBOutlet var dateTF: UITextField!
    
    @IBOutlet var headImageView: UIImageView!
    @IBOutlet var bgImageView: UIImageView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setNavStyle()
        // Do any additional setup after loading the view.
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.setNavigationBarHidden(false, animated: true)
    }
    func setNavStyle(){
        self.title = "口号设置"
        self.addRightBarButton(imageName: "icon_save")
    }
    override func clockRightBarButtonAction() {
        self.view.endEditing(true)
        if self.sloganTF.text!.isEmpty {
            UIView.showTipsView("请输入口号")
            return
        }
        if self.dateTF.text!.isEmpty {
            UIView.showTipsView("请选择需要展示的日期")
            return
        }
        if selectImage == nil {
            UIView.showTipsView("请选择背景图片")
            return
        }
       
        let model = STSloganModel()
        model.day = self.dateTF.text
        model.slogan = self.sloganTF.text
        model.bg = UIImage.saveImageInCache(self.selectImage!)!
        model.isDefault = false
        model.saveToDB()
        UIView.showResultView("口号设置成功", true)
        let helper = STSloganModel.getUsingLKDBHelper()
        helper.delete(with: STSloganModel.self, where: "isDefault = true")
        let delay = 1.5 // 2秒
        let dispatchTime = DispatchTime.now() + delay
        DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
            self.navigationController?.popViewController(animated: true)
        }
    }
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        self.sloganView.addDashedBorder(color: .gray, lineWidth: 2, cornerRadius: 15)
        self.dateView.addDashedBorder(color: .gray, lineWidth: 2, cornerRadius: 15)
        self.photoView.addDashedBorder(color: .gray, lineWidth: 2, cornerRadius: 15)
    }
    
    @IBAction func tapDateSelectionAction(_ sender: Any) {
        self.view.endEditing(true)
        let dialog = Dialog()
        dialog.wMainToBottomSet()(true)
            .wTypeSet()(DialogTypeCalander)
            .wOpenChineseDateSet()(false)
            .wDateTimeTypeSet()("yyyy-MM-dd")
            .wEventOKFinishSet()({(anyId:Any?,otherData:Any?)in
                let selectDay = anyId as! String
                self.dateTF.text = selectDay.toDate(format: "yyyy-MM-dd")?.toString(format: "yyyy-MM-dd")
            })
        _ = dialog.wStart()
    }
    @IBAction func tapAddPictureAction(_ sender: Any) {
        self.view.endEditing(true)
        var config = PickerConfiguration.default
        config.languageType = .simplifiedChinese
        config.maximumSelectedCount = 1
        config.modalPresentationStyle = .fullScreen
        config.selectMode = .single
        config.photoList.allowAddCamera = false
        Photo.picker(
            config
        ) { [self] result, pickerController in
            
            result.getImage(completionHandler: { [self] images in
                bgImageView.image = images.first
                headImageView.image = images.first
                selectImage = images.first
            })
        } cancel: { pickerController in
            // 取消的回调
        }
    }

}
